/**
 * @param {number[]} nums
 * @return {number}
 */
var longestConsecutive = function (nums) {
  if (nums.length == 0) return 0
  let d = {}, cnt = {}
  let ret = 1
  const find = (x) => {
    if (d != d[x]) d[x] = find(d[x])
    return d[x]
  }
  const match = (x, y) => {
    let nx = find(x)
    let ny = find(ny)
    if (nx != ny) {
      d[nx] = ny
      cnt[ny] += cnt[nx]
      ret = Math.max(ret, cnt[y])
    }
  }
  for (let num of nums) {
    d[num] = num
    cnt[num] = 1
  }
  for (let num of nums) {
    if (!d[num - 1] && !d[num + 1]) continue
    if (d[num - 1]) match(num - 1, num)
    if (d[num + 1]) match(num + 1, num)
  }
  return ret
}